#include "xbench/timer.hpp"

using namespace std;


/*
 * Timer
 * */
bool xbench::Timer::start() {
    if(this->_state != TimerCurrentState::Stop)
        // 只有处在Stop状态才能有效调用start方法
        return false;
    this->_state = TimerCurrentState::Run;
    this->_start = chrono::steady_clock::now();
    return true;
}

bool xbench::Timer::cancel() {
    if(this->_state != TimerCurrentState::Run)
        // 只有处在Run状态才能有效调用end方法
        return false;
    this->_state = TimerCurrentState::End;
    this->_end = chrono::steady_clock::now();
    return true;
}

const std::chrono::steady_clock::time_point& xbench::Timer::start_time() const {
    if(this->_state == TimerCurrentState::Stop){
        // todo error
    }
    return this->_start;
}

const std::chrono::steady_clock::time_point &xbench::Timer::end_time() const {
    if(this->_state != TimerCurrentState::End){
        // todo error
    }
    return this->_end;
}

void xbench::Timer::reset() {
    this->_state = TimerCurrentState::Stop;
}

void xbench::Timer::restart() {
    this->reset();
    this->start();
}


/**
 * TimeRecord
 */
xbench::TimerRecord::TimerRecord(const xbench::Timer &timer) {
    this->_start_ns = timer.start_time().time_since_epoch().count();
    this->_duration_ns = timer.duration_ns();
}
